﻿using System.IO;

namespace Common;

public class FileHelper
{
    FileStream _fileStream;

    /// <summary>
    /// 获取文件流
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    /// <exception cref="FileNotFoundException"></exception>
    public byte[] GetFileDate(string filePath)
    {
        if (File.Exists(filePath))
        {
            return File.ReadAllBytes(filePath);
        }
        else
        {
            throw new FileNotFoundException();
        }
    }

    /// <summary>
    /// 保存文件流
    /// </summary>
    /// <param name="fileData"></param>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public bool SaveFileDate(byte[] fileData, string filePath)
    {
        try
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            FileStream fsw = new FileStream(filePath, FileMode.OpenOrCreate);
            //写入
            fsw.Write(fileData, 0, fileData.Length);
            //关闭文件流
            fsw.Close();
            return  true;

        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
            throw;
        }
        

    }

}
